| No. | Answer | Remark | |
|---|---|---|---|
| 1 | a b e f | Prints: XY Prints: YX Nothing is printed. There is no guarantee that the garbage collector will run. | The finalize method of each instance can only run once so X or Y can never be printed more than once. Instances x1 and y1 become eligible for garbage collection when method m returns. Even though System.gc is invoked, there is no guarantee that the garbage collector will run. |
| 2 | a b c | i1 i2 i3 | Objects i1, i2 and i3 form a ring such that each object is referenced by another. Even so, nothing outside of method J.m1 has a reference to i1, i2 or i3. When method J.m1 returns the ring becomes an island of isolated objects that are not reachable by any part of the user program. A key point to remember is that an object that is referenced by another object can be eligible for garbage collection if the two objects form an island of isolated objects. |
| 3 | b c d | 2 3 4 | Method m1 sets all elements of the array to null so the objects created on lines 2, 3 and 4 are all eligible for garbage collection when method m1 returns. |
| 4 | a b | A B | Objects A and B have references to each other but no other Objects refer to A and B. Objects A and B form an island of islolated objects and are eligible for garbage collection. All three references, i1, i2 and i3 refer to Object C so it is not eligible for garbage collection when method m2 begins to execute. |
| 5 | e | Indeterminate. | If method m2 causes a reference to be saved for each Object that is passed in as an argument then none of the objects are eligible for garbage collection when line 3 begins. In this case, we don't know what method m2 might be doing so we can not tell if the objects are eligible for garbage collection. |
| 6 | e | None of the above. | Method m1 sets the argument a1 to null but that does not change the reference a1 in the J.main method. Since array a1 continues to reference all three objects none of the three are eligible for garbage collection. |
| 7 | c | 9 | With each pass through the loop the reference q1 is assigned to a new object and the old object becomes eligible for garbage collection. When the processing of line 2 begins the last object referenced by q1 is not eligible for garbage collection. |
| 8 | c | Prints X. | This is a trick question. The argument, i, of method m1 is a copy of the local variable, i, of method J.main. Setting the argument, i, of method m1 to null does not change the local variable, i, of method J.main. |